
import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入需要的组件
//导入首页，分类，购物车，我的 主组件
import Home from '@/views/home/home.vue';
import Category from '@/views/category/category.vue';
import Cart from '@/views/cart/cart.vue';
import User from '@/views/user/user.vue';
//登陆注册
import Login from '@/views/login/login.vue';
import Register from '@/views/login/register.vue';
//用户详情
import UserDetails from '@/views/user/userDetails.vue';
//用户订单
import UserOrdes from '@/views/user/userOrders.vue';







Vue.use(VueRouter)

const routes = [
  //进页面重定向
  { path: "/", redirect: "/home" },
  //路由规则
  {
    path: "/home",
    component: Home,
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: "/category",
    component: Category,
  },
  {
    path: "/cart",
    component: Cart,
  },
  {
    path: "/user",
    component: User,
    // //子级
    // children: [
    //   { path: "userDetails", component: UserDetails },
    // ]

  },
  {
    path: "/login",
    component: Login,

  },
  {
    path: "/register",
    component: Register,

  }, {//用户详细
    path: "/userDetails",
    component: UserDetails,

  },{
    //用户订单
    path: "/userorders",
    component: UserOrdes,
  }


]

const router = new VueRouter({
  routes
})

//全局前置守卫
router.beforeEach( (to, from, next) => {
  var pathArr=['/cart',]
  if (pathArr.indexOf(to.path) !== -1) {    
    const token = window.sessionStorage.getItem("token");
    if (token) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router

